package com.efei.common.pojo;

import lombok.Data;

import java.util.HashMap;

/**
 * @Author efei
 * 统一相应格式
 */

@Data
public class ResponseData {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int CODE_SUCCESS = 0;
    public static final String String_SUCCESS = "ok";

    /**
     * 错误
     */
    public static final int CODE_ERROR = -1;
    public static final String String_ERROR = "fail";

    /**
     * http 成功
     */
    public static final int SUCCESS = 200;

    /**
     * http 错误
     */
    public static final int ERROR = 500;

    private String msg;
    private int code; // 具体业务数据状态码
    private int  responseStatus; // 数据返回状态码，前端根据这个判断返回是否正常，此码并不是http状态码
    private Object data;

    public ResponseData(Object data, int code, int responseStatus, String msg) {
        put("msg", msg);
        put("code", code);
        put("responseStatus", responseStatus);
        put("data", data);
    }

    public ResponseData(Object data) {
        put("code", CODE_SUCCESS);
        put("responseStatus", SUCCESS);
        put("msg", String_SUCCESS);
        put("data", data);
    }

    public ResponseData() {
        put("code", CODE_SUCCESS);
        put("responseStatus", SUCCESS);
        put("msg", String_SUCCESS);
    }

    public static ResponseData failed(int code, String msg) {
        return new ResponseData("",code,ERROR,msg);
    }

    public static ResponseData failed(String msg) {
        return new ResponseData("",CODE_ERROR,ERROR,msg);
    }

    public static ResponseData ok(Object data) {
        ResponseData r = new ResponseData();
        r.put("data", data);
        return r;
    }

    public static ResponseData ok(Object data, String msg) {
        return ok(data).put("msg", msg);
    }

    public static ResponseData ok(String msg) {
        ResponseData r = new ResponseData();
        r.put("msg", msg);
        r.put("data", "");
        return r;
    }

    public static ResponseData ok() {
        return new ResponseData();
    }


    public ResponseData put(String key, Object value) {
        switch (key){
            case "data":
                this.data=value;
                break;
            case "msg":
                this.msg=(String) value;
                break;
            case "code":
                this.code=Integer.parseInt((String)value);
                break;
            case "responseStatus":
                this.responseStatus=Integer.parseInt((String)value);
                break;
        }
        return this;
    }

    public ResponseData putData(Object value) {
        return this;
    }

    public <T> T getData() {
        T t = (T)data;
        return t;
    }
}
